import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/models/common/LevelOption.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerSchoolAdmin.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/module/openLibraryReadingBody.dart';
import 'package:ichinesereader/widgets/sidebars/adminSidebar.dart';

class OpenReadingBookForSchoolAdminPage extends StatefulWidget {
  static const String routeName = '/schooladmin/library';

  @override
  State<OpenReadingBookForSchoolAdminPage> createState() =>
      _OpenReadingBookForSchoolAdminPageState();
}

class _OpenReadingBookForSchoolAdminPageState
    extends State<OpenReadingBookForSchoolAdminPage> {
  final box = Hive.box('appconfig');
  final BooksController _booksController = Get.find();
  int limit = 20;
  int selectedPageNumber = 1;

  @override
  void initState() {
    if (_booksController.openLibraryReadingBookItems.isNotEmpty) {
      _booksController.openLibraryReadingBookItems.clear();
    }
    // // Future.delayed(Duration(milliseconds: 600), () {
    // //   _booksController.fetchAdminOpenReadingItems(
    // //       limit, selectedPageNumber - 1);
    // // });
    // _booksController.fetchOpenReadingItems(limit, selectedPageNumber - 1);
    super.initState();
  }

  @override
  void dispose() {
    List<LevelOption> levelOptions = [];
    _booksController.setLevelOptions(levelOptions);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build OpenReadingBookForSchoolAdminPage');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.schoolAdminPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar: isSmallScreen ? CustomAppBar(title: 'Library'.tr) : null,
      drawer: isSmallScreen ? SchoolAdminAppDrawer() : null,
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: AdminSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen)),
            Expanded(
              child: Container(
                    margin: _pageStyles.mainContainerWithPaginationEdgeInsets(),
                    child: OpenLibraryReadingBody()
              ),
              //  Container(
              //   margin: _pageStyles.mainContainerWithPaginationEdgeInsets(),
              //   child: Column(
              //     crossAxisAlignment: CrossAxisAlignment.start,
              //     mainAxisAlignment: MainAxisAlignment.start,
              //     children: [
              //       Container(
              //           padding: EdgeInsets.only(left: 9.0),
              //           margin: EdgeInsets.only(bottom: 9.0),
              //           decoration: BoxDecoration(
              //               borderRadius: BorderRadius.circular(6.0),
              //               color: Get.isDarkMode
              //                   ? AppTheme.colors.darkTheme2
              //                   : Colors.white.withOpacity(0.3)),
              //           child: LevelChioceChipWidget(pagingKey: paginationKey,)),
              //       Flexible(
              //         child: Center(
              //           child: _booksController.obx(
              //             (data) => BookGridWithCheckBoxTeaching(
              //               books: _booksController.openLibraryReadingBookItems,
              //               readingType: NanhaiBookReadingTypes.library,
              //             ),
              //             onEmpty: Text('Books not available'.tr),
              //             onLoading: Container(),
              //             // onLoading: Center(
              //             //     child: CircularProgressIndicator(
              //             //   backgroundColor: Get.theme.primaryColorDark,
              //             //   strokeWidth: 9.0,
              //             // )),
              //           ),
              //         ),
              //       ),
              //       Padding(
              //         padding: const EdgeInsets.only(top: 12.0),
              //         child: Center(
              //           child: _booksController.obx(
              //             (data) => NumberPagination(
              //               key: paginationKey,
              //               onPageChanged: (int pageNumber) {
              //                 //got new books for selected page
              //                 setState(() {
              //                   selectedPageNumber = pageNumber;
              //                   _booksController.fetchOpenReadingItems(
              //                       limit, selectedPageNumber - 1);
              //                 });
              //               },
              //               pageTotal: _booksController
              //                               .libraryBooksTotalNum.value %
              //                           limit ==
              //                       0
              //                   ? _booksController.libraryBooksTotalNum.value ~/
              //                       limit
              //                   : _booksController.libraryBooksTotalNum.value ~/
              //                           limit +
              //                       1,
              //               threshold: 5,
              //               pageInit: selectedPageNumber,
              //               // picked number when init page
              //               colorPrimary: Theme.of(context).primaryColorDark,
              //               colorSub: Get.isDarkMode
              //                   ? AppTheme.colors.darkTheme3
              //                   : Colors.white,
              //             ),
              //           ),
              //         ),
              //       ),
              //     ],
              //   ),
              // ),
            ),
          ],
        ),
      ),
    );
  }
}
